﻿/*************************************************************************
* 计算机名：AA-FA-05
* CLR 版本：4.0.30319.42000
* 命名空间: CustomControls.WindowLeftTreeMenuBt
* 类    名：TreeMenuExtensionClosBt
* 当 前 域：HYRC
* 创 建 人：dailuyu
* 创建时间：2021/6/13 14:19:19
* 描    述：
*  V0.01 2021 初版
*
*  Copyright (c) 2021 aatsr Corporation. All rights reserved.
*  ┌──────────────────────────────────┐
*  │　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*  └──────────────────────────────────┘
***************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Animation;


namespace CustomControls.WindowLeftTreeMenuBt
{
    public class TreeMenuExtensionClosBt : ToggleButton
    {
        private DoubleAnimation c_daListAnimation;
        public bool c_changeState = true;//记录菜单栏状态 false隐藏 true显示
        public TranslateTransform Translate { get; set; }



        #region 定义路由事件
        public static readonly RoutedEvent TreeMenuExtensionClosBtRoutedEvent =
            EventManager.RegisterRoutedEvent("TreeMenuExtensionClosBtEvent", RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventArgs>), typeof(TreeMenuExtensionClosBt));

        public event RoutedEventHandler TreeMenuExtensionClosBtEvent
        {
            add { this.AddHandler(TreeMenuExtensionClosBtRoutedEvent, value); }
            remove { this.RemoveHandler(TreeMenuExtensionClosBtRoutedEvent, value); }
        }

        //激发路由事件,借用Click事件的激发方法
        public void OnTreeMenuExtensionClosBtEvent()
        {
            RoutedEventArgs args = new RoutedEventArgs(TreeMenuExtensionClosBtRoutedEvent, this);
            this.RaiseEvent(args);
            ShowHiddenMenu();
        }
        #endregion

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="translate"></param>
        public void Inin(TranslateTransform translate)
        {
            Translate = translate;
            c_daListAnimation = new DoubleAnimation();
            c_daListAnimation.BeginTime = TimeSpan.FromSeconds(1);//获取或设置此 Timeline 将要开始的时间。
            c_daListAnimation.FillBehavior = FillBehavior.HoldEnd;//获取或设置一个值，该值指定 Timeline 在活动周期结束后的行为方式。
            c_daListAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));//获取或设置此时间线播放的时间长度，而不是计数重复
        }

        /// <summary>
        /// 菜单显示与隐藏
        /// </summary>
        public void ShowHiddenMenu()
        {

            if (c_changeState)
            {
                c_changeState = false;
                c_daListAnimation.From = -271;
                c_daListAnimation.To = 0;

            }
            else
            {
                c_changeState = true;
                c_daListAnimation.From = 0;
                c_daListAnimation.To = -271;

            }
            c_daListAnimation.BeginTime = TimeSpan.FromSeconds(0.01);//设置动画将要开始的时间
            SetMoveLocation();
        }

        /// <summary>
        /// 设置移动位置
        /// </summary>
        private void SetMoveLocation()
        {
            if (Translate != null)
            {
                Translate.BeginAnimation(TranslateTransform.XProperty, c_daListAnimation);
            }
        }
    }
}

